package com.goodmean.login;

import com.goodmean.common.User;
import com.jfinal.core.Controller;


public class LoginController extends Controller {
    private static LoginService service = new LoginService().me;
    private static final String RANDOM_CODE_KEY = "code";
    public void index() {
        render("login.html");
    }

    public void login() {
        String loginName = getPara("loginName");
        String password = getPara("password");
        if (validateCaptcha(RANDOM_CODE_KEY )) {

            Integer status = service.login(loginName, password);

            switch (status) {
                case 0: setSession(loginName, status); redirect("admin"); return;
                case 1: setSession(loginName, status); redirect("teacher"); return;
                case 2: setSession(loginName, status); redirect("instructor"); return;
                case 3: setSession(loginName, status); redirect("student"); return;
                default: setAttr("loginError", "账号或密码出错了!");break;
            }
        } else {
            setAttr("codeError", "验证码出错了!");
        }
    }
    
    public void code(){

        renderCaptcha();
    }



    private void setSession(String loginName , Integer status){
        User user = new User(loginName , status);
        setSessionAttr("user",user);
        setAttr("user" , user);
    }
    public void quit(){
        removeSessionAttr("user");
        render("login.html");
    }

    public void error(){
	    render("login_failed.html");
    }
}
